let express = require('express');
let bodyParser = require('body-parser');
let config = require('./config');
let logger = require('./src/middleware/operate-log');
let authorization = require('./src/middleware/authorization');
let token = require('./src/middleware/token');
let apiRoutes = require('./src/routes/index');
let loginRoute = require('./src/routes/login');

// 创建一个express实例
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// 验证token
app.use(authorization);
// 操作日志 中间件
app.use(logger);
// 路由
app.use('/api', apiRoutes);
app.use('/login', loginRoute);
// 生成token
app.use(token);
// 处理最终数据的中间件
app.use(function (req, res) {
  res.send(res._sendData);
});
app.use(function (err, req, res, next) {
  res.send(err);
});

app.listen(config.port, function (err) {

  if (err) {
    console.log(err);
    return;
  }

  console.log('Server is running...');

  console.log('Host:127.0.0.1');

  console.log(`Port:${config.port}`);
});